package boundary;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import dataview.UnitaPorzioni;
import exceptions.DBException;
import store.Cibo;
import store.Frammento;
import store.Passo;
import store.Ricetta;
import store.RifIngrediente;
import store.RifRicetta;
import store.Testo;
import store.TutteRicette;
import store.TuttiCibi;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import javax.swing.ImageIcon;

public class InserisciUnPasso extends JDialog implements ActionListener, MouseListener {
	
	private static final long serialVersionUID = 1L;
	private ListaPassi lista;
	private int pos;
	private int framStart, framEnd;
	private Passo passo;
	private Passo aux; //  @jve:decl-index=0:
	private final static Color INGREDIENTE = Color.GREEN; //  @jve:decl-index=0:
	private final static Color RICETTA = Color.PINK; //  @jve:decl-index=0:
	private Highlighter hilit;
	private Highlighter.HighlightPainter painter; //  @jve:decl-index=0:
	private boolean modifica;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JButton testoB = null;
	private JTextField testoTF = null;
	private JButton ingredienteB = null;
	private JTextField qCiboTF = null;
	private JComboBox ciboCB = null;
	private JButton ricettaB = null;
	private JComboBox ricettaCB = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextArea = null;
	private JButton annullaB = null;
	private JButton finitoB = null;
	private JTextField qRicettaTF = null;
	private JPanel jPanel1 = null;
	private JPanel pInsIngr = null;
	private JLabel lQtaIngr = null;
	private JLabel lUnitaMis = null;
	private JPanel pInsSottoric = null;
	private JLabel lInsQtaRic = null;
	private JLabel lUnitaMisRic = null;
	private JButton eliminaB = null;
	private Frammento frammentoSelezionato = null;
	
	/**
	 * @param owner
	 */
	public InserisciUnPasso(Frame owner, ListaPassi lista, int pos) {
		super(owner);
		this.lista = lista;
		this.pos = pos;
		this.modifica = false;
		passo = new Passo();
		initialize();
	}
	
	public InserisciUnPasso(Frame owner, ListaPassi lista, int pos, Passo passo) {
		super(owner);
		this.lista = lista;
		this.pos = pos;
		this.passo = passo;
		this.modifica = true;
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		setAuxPasso();
		hilit = new DefaultHighlighter();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (d.width - 600) / 2;
		int y = (d.height - 460) / 2;
		this.setSize(600, 460);
		this.setPreferredSize(new Dimension(600, 460));
		this.setMinimumSize(new Dimension(600, 460));
		this.setLocation(x, y);
		this.setLocationRelativeTo(getOwner());
		this.setTitle("Inserisci un passo");
		this.setModal(true);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setContentPane(getJContentPane());
		pack();
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(5);
			borderLayout.setVgap(5);
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 0;
			gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints15.gridy = 1;
			jLabel = new JLabel();
			jLabel.setText("Inserisci un passo");
			jLabel.setFont(new Font("Dialog", Font.BOLD, 20));
			jLabel.setPreferredSize(new Dimension(172, 36));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jContentPane = new JPanel();
			jContentPane.setLayout(borderLayout);
			jContentPane.add(jLabel, BorderLayout.NORTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 11;
			gridBagConstraints16.insets = new Insets(5, 10, 10, 10);
			gridBagConstraints16.anchor = GridBagConstraints.WEST;
			gridBagConstraints16.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints16.gridy = 16;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints14.gridwidth = 6;
			gridBagConstraints14.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints14.gridy = 2;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridwidth = 6;
			gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints10.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints10.gridy = 1;
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridx = 0;
			gridBagConstraints22.gridwidth = 6;
			gridBagConstraints22.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints22.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints22.gridy = 0;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 1;
			gridBagConstraints31.anchor = GridBagConstraints.CENTER;
			gridBagConstraints31.insets = new Insets(5, 10, 10, 10);
			gridBagConstraints31.gridy = 16;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 2;
			gridBagConstraints21.anchor = GridBagConstraints.EAST;
			gridBagConstraints21.insets = new Insets(5, 10, 10, 10);
			gridBagConstraints21.fill = GridBagConstraints.NONE;
			gridBagConstraints21.gridy = 16;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridy = 0;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.weighty = 1.0;
			gridBagConstraints7.gridwidth = 5;
			gridBagConstraints7.gridheight = 8;
			gridBagConstraints7.gridx = 7;
			gridBagConstraints7.insets = new Insets(5, 5, 5, 10);
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJScrollPane(), gridBagConstraints7);
			jPanel.add(getAnnullaB(), gridBagConstraints21);
			jPanel.add(getFinitoB(), gridBagConstraints31);
			jPanel.add(getJPanel1(), gridBagConstraints22);
			jPanel.add(getPInsIngr(), gridBagConstraints10);
			jPanel.add(getPInsSottoric(), gridBagConstraints14);
			jPanel.add(getEliminaB(), gridBagConstraints16);
		}
		return jPanel;
	}
	
	/**
	 * This method initializes testoB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getTestoB() {
		if (testoB == null) {
			testoB = new JButton();
			testoB.setText("Inserisci");
			testoB.setFont(new Font("Dialog", Font.BOLD, 12));
			testoB.setIcon(new ImageIcon(getClass().getResource("/icone/folder-full-add-icon.png")));
			testoB.setEnabled(false);
			testoB.addActionListener(this);
		}
		return testoB;
	}
	
	/**
	 * This method initializes testoTF
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTestoTF() {
		if (testoTF == null) {
			testoTF = new JTextField();
			testoTF.setColumns(10);
			testoTF.setText("");
			testoTF.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent k) {
					if (testoTF.getText().length() == 0)
						getTestoB().setEnabled(false);
					else if (!getTestoB().isEnabled()) getTestoB().setEnabled(true);
				}
			});
		}
		return testoTF;
	}
	
	/**
	 * This method initializes ingredienteB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getIngredienteB() {
		if (ingredienteB == null) {
			ingredienteB = new JButton();
			ingredienteB.setFont(new Font("Dialog", Font.BOLD, 12));
			ingredienteB.setText("Inserisci");
			ingredienteB.setIcon(new ImageIcon(getClass().getResource("/icone/folder-full-add-icon.png")));
			ingredienteB.setEnabled(false);
			ingredienteB.addActionListener(this);
		}
		return ingredienteB;
	}
	
	/**
	 * This method initializes qCiboTF
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getQCiboTF() {
		if (qCiboTF == null) {
			qCiboTF = new JTextField();
			qCiboTF.setColumns(5);
			qCiboTF.setText("");
			qCiboTF.setEnabled(false);
			qCiboTF.addKeyListener(new NumericAdapter());
			qCiboTF.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent k) {
					if (qCiboTF.getText().length() == 0)
						getIngredienteB().setEnabled(false);
					else {
						int n = 0;
						try {
							n = Integer.parseInt(qCiboTF.getText());
						} catch (NumberFormatException e) {}
						if (n <= 0)
							getIngredienteB().setEnabled(false);
						else if (!getIngredienteB().isEnabled()) getIngredienteB().setEnabled(true);
					}
				}
			});
		}
		return qCiboTF;
	}
	
	/**
	 * This method initializes ciboCB
	 * 
	 * @return javax.swing.JComboBox
	 */
	@SuppressWarnings("unchecked")
	private JComboBox getCiboCB() {
		if (ciboCB == null) {
			List list = new LinkedList();
			list.add("Seleziona ingrediente..");
			try {
				list.addAll(TuttiCibi.getTuttiCibi().tuttiCibi());
			} catch (DBException e) {
				System.out.println(e);
			}
			ciboCB = new JComboBox(list.toArray());
			
			ciboCB.addActionListener(this);
		}
		return ciboCB;
	}
	
	/**
	 * This method initializes ricettaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRicettaB() {
		if (ricettaB == null) {
			ricettaB = new JButton();
			ricettaB.setFont(new Font("Dialog", Font.BOLD, 12));
			ricettaB.setText("Inserisci");
			ricettaB.setIcon(new ImageIcon(getClass().getResource("/icone/folder-full-add-icon.png")));
			ricettaB.setEnabled(false);
			ricettaB.addActionListener(this);
		}
		return ricettaB;
	}
	
	/**
	 * This method initializes ricettaCB
	 * 
	 * @return javax.swing.JComboBox
	 */
	@SuppressWarnings("unchecked")
	private JComboBox getRicettaCB() {
		if (ricettaCB == null) {
			List list = new LinkedList();
			list.add("Seleziona ricetta..");
			try {
				list.addAll(TutteRicette.getTutteRicette().tutteRicette());
			} catch (DBException e) {
				System.out.println(e);
			}
			ricettaCB = new JComboBox(list.toArray());
			
			ricettaCB.addActionListener(this);
		}
		return ricettaCB;
	}
	
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}
	
	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setLineWrap(true);
			jTextArea.setEditable(false);
			jTextArea.setHighlighter(hilit);
			jTextArea.addMouseListener(this);
			jTextArea.setFont(new Font("Dialog", Font.PLAIN, 12));
			jTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					if (e.getOppositeComponent() == getEliminaB() && getJTextArea().getSelectedText() != null)
						getEliminaB().setEnabled(true);
					else getEliminaB().setEnabled(false);
					updateHighlighting(-1);
				}
			});
			updateHighlighting(-1);
		}
		return jTextArea;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == getCiboCB()) {
			getIngredienteB().setEnabled(false);
			if (getCiboCB().getSelectedIndex() > 0) {
				getQCiboTF().setEnabled(true);
				if (((Cibo)getCiboCB().getSelectedItem()).getInfo().getUnitaRiferimento() != null)
					lUnitaMis.setText(((Cibo)getCiboCB().getSelectedItem()).getInfo().getUnitaRiferimento().toStringShort());
				else {
					lUnitaMis.setText("");
					getIngredienteB().setEnabled(true);
				}
			} else if (getCiboCB().getSelectedIndex() == 0) {
				getQCiboTF().setEnabled(false);
				lUnitaMis.setText("");
			}
		}
		if (e.getSource() == getRicettaCB()) {
			if (getRicettaCB().getSelectedIndex() > 0) {
				getQRicettaTF().setEnabled(true);
				if (((Ricetta)getRicettaCB().getSelectedItem()).getInfo().getUnitaPorzioni() == UnitaPorzioni.altro) {
					if (((Ricetta)getRicettaCB().getSelectedItem()).getInfo().getCustom().isEmpty())
						lUnitaMisRic.setText("porzioni");
					else lUnitaMisRic.setText(((Ricetta)getRicettaCB().getSelectedItem()).getInfo().getCustom());
				} else lUnitaMisRic.setText(((Ricetta)getRicettaCB().getSelectedItem()).getInfo().getUnitaPorzioni().toString());
			} else if (getRicettaCB().getSelectedIndex() == 0) {
				getQRicettaTF().setEnabled(false);
				lUnitaMisRic.setText("");
			}
		}
		if (e.getSource() == getTestoB()) { // inserisci testo
			String text = getTestoTF().getText();
			if (text != null && text.length() > 0) {
				aux.aggiungiFrammentoTesto(text);
				getTestoTF().setText("");
				if (!getFinitoB().isEnabled()) getFinitoB().setEnabled(true);
			}
			getTestoB().setEnabled(false);
		} else if (e.getSource() == getIngredienteB()) { // inserisci quant + ingrediente
			String quant = getQCiboTF().getText();
			if (getCiboCB().getSelectedIndex() > 0) {
				int nQuant;
				try {
					if (((Cibo)getCiboCB().getSelectedItem()).getInfo().getUnitaRiferimento() == null && quant.isEmpty())
						nQuant = 0;
					else nQuant = Integer.parseInt(quant);
					aux.aggiungiFrammentoIngrediente((double)nQuant, (Cibo)getCiboCB().getSelectedItem());
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
				}
				getQCiboTF().setText("");
				getCiboCB().setSelectedIndex(0);
				if (!getFinitoB().isEnabled()) getFinitoB().setEnabled(true);
			}
			getIngredienteB().setEnabled(false);
		} else if (e.getSource() == getRicettaB()) { // inserisci sottoricetta
			String porz = getQRicettaTF().getText();
			if (porz != null && porz.length() > 0 && getRicettaCB().getSelectedIndex() > 0) {
				try {
					int nPorz = Integer.parseInt(porz);
					aux.aggiungiFrammentoRicetta(nPorz, (Ricetta)getRicettaCB().getSelectedItem());
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
				}
				getQRicettaTF().setText("");
				getRicettaCB().setSelectedIndex(0);
				if (!getFinitoB().isEnabled()) getFinitoB().setEnabled(true);
			}
			getRicettaB().setEnabled(false);
		} else if (e.getSource() == getAnnullaB()) { // annulla inserimento passo
			dispose();
		} else if (e.getSource() == getFinitoB()) { // conferma inserimento
			int res = JOptionPane.showConfirmDialog(this, "Confermi l'inserimento del passo?", "Conferma", JOptionPane.OK_CANCEL_OPTION);
			if (res == JOptionPane.OK_OPTION) {
				passo.getFrammenti().clear();
				for (Frammento f : aux.getFrammenti()) {
					if (f instanceof Testo)
						passo.aggiungiFrammentoTesto(((Testo)f).getTesto());
					else if (f instanceof RifIngrediente)
						passo.aggiungiFrammentoIngrediente(((RifIngrediente)f).getQuantita(), ((RifIngrediente)f).getIngrediente());
					else passo.aggiungiFrammentoRicetta(((RifRicetta)f).getnPorzioni(), ((RifRicetta)f).getRicetta());
				}
				if (!modifica)
					lista.passaPasso(passo, pos);
				else lista.passoModificato();
				dispose();
			}
		} else if (e.getSource() == getEliminaB()) {
			if (frammentoSelezionato != null) {
				int res = JOptionPane.showConfirmDialog(this, "Eliminare questo frammento del passo della ricetta?", "Conferma eliminazione", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (res == JOptionPane.OK_OPTION) {
					aux.getFrammenti().remove(frammentoSelezionato);
					if (aux.getFrammenti().size() < 1) {
						getFinitoB().setEnabled(false);
					}
					getEliminaB().setEnabled(false);
					updateHighlighting(-1);
				} else {
					getJTextArea().setSelectionStart(0);
					getJTextArea().setSelectionEnd(0);
				}
				frammentoSelezionato = null;
			}
		}
		
		updateHighlighting(-1);
	}
	
	private void updateHighlighting(Integer eccetto) {
		hilit.removeAllHighlights();
		jTextArea.setText("");
		int i = 0;
		for (Frammento f : aux.getFrammenti()) {
			jTextArea.append(f.toString());
			++i;
		}
		i = 0;
		for (Frammento f : aux.getFrammenti()) {
			if (eccetto < aux.indiceInizioFrammento(i) || eccetto > aux.indiceFineFrammento(i)) {
			if (f instanceof RifIngrediente) {
					painter = new DefaultHighlighter.DefaultHighlightPainter(INGREDIENTE);
					try {
						hilit.addHighlight(aux.indiceInizioFrammento(i), aux.indiceFineFrammento(i), painter);
					} catch (BadLocationException e) {
						e.printStackTrace();
					}
				} else if (f instanceof RifRicetta) {
					painter = new DefaultHighlighter.DefaultHighlightPainter(RICETTA);
					try {
						hilit.addHighlight(aux.indiceInizioFrammento(i), aux.indiceFineFrammento(i), painter);
					} catch (BadLocationException e) {
						e.printStackTrace();
					}
				}
			}
			++i;
		}
	}
	
	private void setAuxPasso() {
		aux = new Passo();
		if (passo == null) return;
		for (Frammento f : passo.getFrammenti()) {
			if (f instanceof Testo)
				aux.aggiungiFrammentoTesto(((Testo)f).getTesto());
			else if (f instanceof RifIngrediente)
				aux.aggiungiFrammentoIngrediente(((RifIngrediente)f).getQuantita(), ((RifIngrediente)f).getIngrediente());
			else aux.aggiungiFrammentoRicetta(((RifRicetta)f).getnPorzioni(), ((RifRicetta)f).getRicetta());
		}
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {}
	
	@Override
	public void mouseExited(MouseEvent e) {}
	
	@Override
	public void mousePressed(MouseEvent e) {}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		selectFrammentoFromCaret();
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {}
	
	private void selectFrammentoFromCaret() {
		int c = getJTextArea().getCaretPosition();
		for (int j = 0; j < aux.getFrammenti().size(); j++) {
			framStart = aux.indiceInizioFrammento(j);
			framEnd = aux.indiceFineFrammento(j);
			if (c >= framStart && c < framEnd) // FIXME: occhio agli indici del cipo 
			{
				getEliminaB().setEnabled(true);
				frammentoSelezionato = aux.getFrammenti().get(j);
				updateHighlighting(c);
				getJTextArea().setSelectionStart(framStart);
				getJTextArea().setSelectionEnd(framEnd);
				return;
			}
		}
		frammentoSelezionato = null;
		getEliminaB().setEnabled(false);
	}
	
	/**
	 * This method initializes annullaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAnnullaB() {
		if (annullaB == null) {
			annullaB = new JButton();
			annullaB.setText("Annulla");
			annullaB.setFont(new Font("Dialog", Font.BOLD, 12));
			annullaB.setIcon(new ImageIcon(getClass().getResource("/icone/remove-icon.png")));
			annullaB.addActionListener(this);
		}
		return annullaB;
	}
	
	/**
	 * This method initializes finitoB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getFinitoB() {
		if (finitoB == null) {
			finitoB = new JButton();
			finitoB.setText("Finito");
			finitoB.setFont(new Font("Dialog", Font.BOLD, 12));
			finitoB.setIcon(new ImageIcon(getClass().getResource("/icone/accept-icon.png")));
			finitoB.setEnabled(aux.getFrammenti().size() > 0);
			finitoB.addActionListener(this);
		}
		return finitoB;
	}
	
	/**
	 * This method initializes qRicettaTF
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getQRicettaTF() {
		if (qRicettaTF == null) {
			qRicettaTF = new JTextField();
			qRicettaTF.setColumns(5);
			qCiboTF.setText("");
			qRicettaTF.setEnabled(false);
			qRicettaTF.addKeyListener(new NumericAdapter());
			qRicettaTF.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent k) {
					if (qRicettaTF.getText().length() == 0)
						getRicettaB().setEnabled(false);
					else {
						int n = 0;
						try {
							n = Integer.parseInt(qRicettaTF.getText());
						} catch (NumberFormatException e) {}
						if (n <= 0)
							getRicettaB().setEnabled(false);
						else if (!getRicettaB().isEnabled()) getRicettaB().setEnabled(true);
					}
				}
			});
		}
		return qRicettaTF;
	}
	
	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.insets = new Insets(3, 0, 3, 0);
			gridBagConstraints.gridy = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.insets = new Insets(3, 0, 3, 0);
			gridBagConstraints1.gridwidth = 2;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.setBorder(BorderFactory.createTitledBorder(null, "Inserisci testo", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			jPanel1.add(getTestoTF(), gridBagConstraints1);
			jPanel1.add(getTestoB(), gridBagConstraints);
		}
		return jPanel1;
	}
	
	/**
	 * This method initializes pInsIngr
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPInsIngr() {
		if (pInsIngr == null) {
			TitledBorder titledBorder1 = BorderFactory.createTitledBorder(null, "Inserisci ingrediente", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridwidth = 3;
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.insets = new Insets(3, 0, 3, 0);
			gridBagConstraints2.gridx = 0;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 2;
			gridBagConstraints9.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridwidth = 1;
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridy = 1;
			lUnitaMis = new JLabel();
			lUnitaMis.setText("");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.anchor = GridBagConstraints.WEST;
			gridBagConstraints8.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints8.gridy = 1;
			lQtaIngr = new JLabel();
			lQtaIngr.setText("Inserisci quantita'");
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints4.anchor = GridBagConstraints.WEST;
			gridBagConstraints4.gridwidth = 3;
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.anchor = GridBagConstraints.EAST;
			gridBagConstraints3.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints3.gridwidth = 1;
			gridBagConstraints3.gridx = 1;
			pInsIngr = new JPanel();
			pInsIngr.setLayout(new GridBagLayout());
			pInsIngr.setBorder(titledBorder1);
			pInsIngr.add(getQCiboTF(), gridBagConstraints3);
			pInsIngr.add(getCiboCB(), gridBagConstraints4);
			pInsIngr.add(lQtaIngr, gridBagConstraints8);
			pInsIngr.add(lUnitaMis, gridBagConstraints9);
			pInsIngr.add(getIngredienteB(), gridBagConstraints2);
		}
		return pInsIngr;
	}
	
	/**
	 * This method initializes pInsSottoric
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPInsSottoric() {
		if (pInsSottoric == null) {
			TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "Inserisci sottoricetta", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null);
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridwidth = 3;
			gridBagConstraints5.gridy = 2;
			gridBagConstraints5.insets = new Insets(3, 0, 3, 0);
			gridBagConstraints5.fill = GridBagConstraints.NONE;
			gridBagConstraints5.gridx = 0;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 2;
			gridBagConstraints13.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints13.anchor = GridBagConstraints.WEST;
			gridBagConstraints13.gridwidth = 1;
			gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints13.gridy = 1;
			lUnitaMisRic = new JLabel();
			lUnitaMisRic.setText("");
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.gridy = 1;
			lInsQtaRic = new JLabel();
			lInsQtaRic.setText("Inserisci quantita'");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.gridy = 1;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.gridwidth = 1;
			gridBagConstraints11.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridwidth = 3;
			gridBagConstraints6.gridx = 0;
			pInsSottoric = new JPanel();
			pInsSottoric.setLayout(new GridBagLayout());
			pInsSottoric.setBorder(titledBorder);
			pInsSottoric.add(getRicettaCB(), gridBagConstraints6);
			pInsSottoric.add(getQRicettaTF(), gridBagConstraints11);
			pInsSottoric.add(lInsQtaRic, gridBagConstraints12);
			pInsSottoric.add(lUnitaMisRic, gridBagConstraints13);
			pInsSottoric.add(getRicettaB(), gridBagConstraints5);
		}
		return pInsSottoric;
	}
	
	/**
	 * This method initializes eliminaB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getEliminaB() {
		if (eliminaB == null) {
			eliminaB = new JButton();
			eliminaB.setText("Elimina Frammento");
			eliminaB.setFont(new Font("Dialog", Font.BOLD, 12));
			eliminaB.setToolTipText("Per eliminare un frammento cliccaci sopra col mouse e poi clicca su questo bottone");
			eliminaB.setEnabled(false);
			eliminaB.addActionListener(this);
			
		}
		return eliminaB;
	}
} //  @jve:decl-index=0:visual-constraint="10,10"
